import {
  imageUrl
} from "../../utils/imageutil"
Component({
  properties: {
    showPopup: {
      type: Boolean,
      value: false
    },
    popupColor: Boolean,
    price: {
      type: String,
      value: ''
    },
    selling_intro: {
      type: String,
      value: ''
    },
    img_path: {
      type: String,
      value: ''
    },
    goodsName: String,
    curSku: Object,
    skuList: Array,
    specList: {
      type: Array,
      value: []
    },
    skuVname: String,
    skuVal: String,
    specArray: {
      type: Array,
      value: []
    },
    curSpec: String,
    buyNum: {
      type: Number,
      value: 1,
    },

  },
  data: {
    imageUrl,
    showPopup: false,
    num: 1,
    id: "",
    ggsArr: []
  },
  methods: {
    close() {
      this.setData({
        showPopup: false
      })
    },
    determine() {
      // 确定按钮
      if (this.data.specList.length == this.data.specArray.length) {
        let num = 0
        if (this.data.specArray.length == this.data.specList.length) {
          this.data.specArray.forEach((v, i) => {
            num += 1
          })
        }
        if (num === this.data.specList.length) {
          this.triggerEvent('determine', true)
        }else{
          wx.showToast({
            title: '请选择所有规格',
            icon: 'none'
          })
        }
      } else {
        wx.showToast({
          title: '请选择所有规格',
          icon: 'none'
        })
      }
    },
    //监听radio变化事件
    onChangeSpec(event) {
      const {
        specArray,
        skuList,
        specList,
        ggsArr
      } = this.data;
      specArray[event.currentTarget.dataset.index] = this.data.specList[event.currentTarget.dataset.index].specValList[event.currentTarget.dataset.i].specValId;
      ggsArr[event.currentTarget.dataset.index] = this.data.specList[event.currentTarget.dataset.index].specValList[event.currentTarget.dataset.i].specValName;
      const {
        buyNum,
        goodsName,
      } = this.data;
      this.setData({
        specArray: specArray,
      });

      let num = 0
      if (specArray.length == this.data.specList.length) {
        specArray.forEach((v, i) => {
          num += 1
        })
      }
      if (num === this.data.specList.length) {
        this.triggerEvent('changeSpec', {
          specArray: this.data.specArray,
          ggsArr,
        })
      }
      return
      const skuVal = specArray.join(";");
      // 提示用户未选择什么规格
      const unSlectedSku = specList.filter(item => {
        return item.specId != event.currentTarget.dataset.specid
      })[0];
      if (unSlectedSku != undefined) {
        const skuTip = "选择 " + unSlectedSku.specName;
        this.setData({
          curSpec: skuTip
        })
      }
      console.log(this.data)
      const curSku = skuList.filter(item => (item.skuVal === skuVal))[0];
      // 找到当前规格的商品信息后，更新商品价钱、图片等信息

      console.log(curSku)

      if (curSku != undefined) {
        this.setData({
          img_path: curSku.imagePath,
          price: curSku.price,
          curSpec: '已选：' + curSku.skuVname,
          curSku: {
            ...curSku,
            goodsName: goodsName,
            buyNum: buyNum,
          },
        });
      }
    },
  }
})